﻿using Config.Common.Utils;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Project.Core.Config.Common.Lang;
using Project.Core.Config.Common.Utils;
using Project.Core.Config.Config.Mq;
using Project.Core.Config.Mapper;
using Project.Core.Test.Entity;
using System.Diagnostics;
using Project.Core.Test.Entity.Input;
using Microsoft.Extensions.Logging;

namespace Project.Core.Test.Controllers {
    /// <summary>
    /// 测试接口
    /// </summary>
    public class ATestController : TestController<ATestController> {
        public BaseMapper<SysBook, int> BaseMapper { get; set; }
        public IMqConnectionHelper MqConnection { get; set; }

        /// <summary>
        /// 测试接口
        /// </summary>
        [HttpPost]
        [Route("/test/test")]
        public Result Test(SysBook input) {
            var list = SysBookService.Update(input);
            return Result.Succ(input);
        }
        /// <summary>
        /// 测试异步接口
        /// </summary>
        [HttpPost]
        [Route("/test/testAsync")]
        public async Task<Result> TestAsync(List<int> input) {
            var list = await SysBookService.DeleteBatchIdsAsync(input);
            return Result.Succ(list);
        }

        /// <summary>
        /// 测试Mq生产者
        /// </summary>
        [HttpPost]
        [Route("/test/testMqPro")]
        public async Task<Result> TestMqPro() {
            // 读取配置信息
            MqConnectionHelper.MqConfig = AppHelper.ReadAppSettings<MqConfigInfo>("Mq", "MqConfig");

            Debug.WriteLine("开始生产者连接");
            MqConnection.SendFactoryConnectionInit();
            MqConnection.SendFactoryConnection();

            Debug.WriteLine("生产者连接完成，准备发送消息");
            MqConnectionHelper.PublishExchange("Hello,MQ~~", MqConnectionHelper.MqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper.RoutingKey[0]);
            MqConnectionHelper.PublishExchange("Hello,MQ~~", MqConnectionHelper.MqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper.RoutingKey[1]);

            Debug.WriteLine("消息发送完成");

            return Result.Succ("消息发送完成");
        }
        /// <summary>
        /// 测试Mq消费者
        /// </summary>
        [HttpPost]
        [Route("/test/testMqCon")]
        public async Task<Result> TestMqCon() {
            // 读取配置信息
            MqConnectionHelper.MqConfig = AppHelper.ReadAppSettings<MqConfigInfo>("Mq", "MqConfig");

            Debug.WriteLine("开始消费者连接");
            MqConnection.ReceiveFactoryConnectionInit();
            for (int i = 0; i < MqConnectionHelper.CostomerCount; i++) {
                MqConnection.ConnectionReceive(i, MqConnectionHelper.MqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper.RoutingKey[i], MqConnectionHelper.QueueName[i]);
            }

            return Result.Succ("消费者");
        }
        /// <summary>
        /// 测试Mq生产消费者
        /// </summary>
        [HttpPost]
        [Route("/test/testMqProCon")]
        public async Task<Result> TestMqProCon() {
            // 读取配置信息
            MqConnectionHelper.MqConfig = AppHelper.ReadAppSettings<MqConfigInfo>("Mq", "MqConfig");

            Debug.WriteLine("开始生产者连接");
            MqConnection.SendFactoryConnectionInit();
            MqConnection.SendFactoryConnection();

            Debug.WriteLine("生产者连接完成，开始连接消费者");
            MqConnection.ReceiveFactoryConnectionInit();
            for (int i = 0; i < MqConnectionHelper.CostomerCount; i++) {
                MqConnection.ConnectionReceive(i, MqConnectionHelper.MqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper.RoutingKey[i], MqConnectionHelper.QueueName[i]);
            }
            
            Debug.WriteLine("消费者连接完成，生产者准备发送消息");
            MqConnectionHelper.PublishExchange("Hello,MQ~~", MqConnectionHelper.MqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper.RoutingKey[0]);
            MqConnectionHelper.PublishExchange("Hello,MQ~~", MqConnectionHelper.MqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper.RoutingKey[1]);
            //for (int i = 0; i < 100; i++) {
            //    if (i % 2 == 0) {
            //        MqConnectionHelper.PublishExchange("Hello,MQ~~", MqConnectionHelper._mqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper._routingKey[0]);
            //    } else {
            //        MqConnectionHelper.PublishExchange("Hello,MQ~~", MqConnectionHelper._mqConfig.FirstOrDefault().ExchangeName, MqConnectionHelper._routingKey[1]);
            //    }
            //    //Thread.Sleep(500);
            //}

            Debug.WriteLine("消息发送完成");

            return Result.Succ("消息发送完成");
        }

        /// <summary>
        /// 测试PayTest
        /// </summary>
        [HttpGet]
        [Route("/test/testPay")]
        public async Task<Result> TestPay(string number, int total) {
            string prepayId = await WxApi.JsapiOrder("QQ公仔", number, total, "oeRwP5ZCtp3dvloAabIqLD1VecYc");
            var json = (JObject)JsonConvert.DeserializeObject(prepayId);
            Debug.WriteLine(prepayId);
            if (json["prepay_id"] == null) {
                return Result.Fail(400, "信息有误", JsonConvert.DeserializeObject(prepayId));
            } else {
                var data = WxApi.Pay(json["prepay_id"].ToString());
                return Result.Succ(JsonConvert.DeserializeObject(data));
            }
        }
        /// <summary>
        /// 测试refund
        /// </summary>
        [HttpPost]
        [Route("/test/testRefund")]
        public async Task<Result> TestRefund(WxRefundInput refundData) {
            string refundStr = await WxApi.Refund(refundData.TradeNo, DateTime.Now.ToString("yyyyMMddHHmmssfff"), refundData.Reason, refundData.Refund, refundData.Total, refundData.GoodsId, refundData.UnitPrice, refundData.RefundAmount, refundData.RefundQuantity);
            var refundJson = (JObject)JsonConvert.DeserializeObject(refundStr);

            if (refundJson["status"] != null && refundJson["status"].ToString() == "SUCCESS") {
                return Result.Succ(refundJson);
            } else {
                return Result.Fail(400, "出错了", refundJson);
            }
        }

        /// <summary>
        /// 测试保存图片
        /// </summary>
        [HttpGet]
        [Route("/test/testSaveImage")]
        public Result TestSaveImage(string fileName, string? pathName) {
            Debug.WriteLine("测试保存图片");
            var path = Producer.SaveImage(Configuration, "",
                fileName, pathName);
            return Result.Succ(path);
        }
    }
}
